package com.example.text.ui.mall.view

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.widget.AppCompatButton

/**
 * Created by ZengCS on 2022/7/28.
 * E-mail:zengcs@vip.qq.com
 * Add:中国成都
 * Desc: 点击后自动缩放的Button
 */
class ScalableButton(context: Context, attrs: AttributeSet?, defStyle: Int) :
    AppCompatButton(context, attrs, defStyle), View.OnTouchListener {
    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    companion object {
        private const val SCALE = 0.95f
    }

    init {
        setOnTouchListener(this)
    }

    override fun onTouch(v: View?, event: MotionEvent): Boolean {
        return if (isClickable && isFocusable) {
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    scaleX = SCALE
                    scaleY = SCALE
                }
                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    scaleX = 1f
                    scaleY = 1f
                }
            }
            when (event.actionMasked) {
                MotionEvent.ACTION_CANCEL -> {
                    scaleX = 1f
                    scaleY = 1f
                }
            }
            false
        } else {
            super.onTouchEvent(event)
        }
    }
}